//author:kakake
class tsregisterController {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        WorkId: 1,
        AppId: 1
    }

    public hisinterface: tshisinterfaceController;
    public service: tsregisterService;
    public dao: tsregisterDao;
    //界面医生列表模型
    public vo_doctorlist = {
        DoctId: 0,
        DeptId: 0,
        RegDate: '',//挂号日期
        HeadImg: '',//头像
        RankName: '',//主任医师
        DeptName: '',//科室
        DocName: '',//医生名称
        Describe: '',//介绍、擅长
        IsHave: 0,//有号和无号
        IsFull: 0,//有号是否约满
        AllCount: 0,//总号源
        HaveCount: 0//剩余号源数
    }
    //界面医生页面排班模型
    public vo_doctorschedule = {
        DoctId: 0,
        DeptId: 0,
        DeptName: '',//科室
        DocName: '',//医生名称
        RegId: 0,//排班记录id
        Week: '',
        RegFee: 0,
        TimeType: 1,//1上午 2下午 3晚上
        RegDate: '',//挂号日期
        IsHave: 0,//有号和无号
        IsFull: 0,//有号是否约满
        AllCount: 0,//总号源
        HaveCount: 0//剩余号源数
    }
    //界面显示号源时段，可以配置成精确到每小时
    public vo_doctorrange = {
        Timeid: 0,//排班时段id
        RegId: 0,//排班记录id
        DoctId: 0,
        DeptId: 0,
        DeptName: '',//科室
        DocName: '',//医生名称
        RegDate: '',//挂号日期
        IsHave: 0,//有号和无号
        HaveCount: 0,//剩余号源数
        AllCount: 0,//总号源
        IsAll: 0,//是否全部
        Status: 0,//状态0正常 1停用
        IsExpire: 0,//过期0正常 1过期
        Week: '',
        RegFee: 0,
        TimeType: 1,//1上午 2下午 3晚上
        StartTime: '',
        EndTime: '',
    }



    constructor() {
        this.data.WorkId = TSLobsterAPI.Program.GetWorkId();

        this.hisinterface = new tshisinterfaceController(1);
        this.service = new tsregisterService(this.data.WorkId);
        this.dao = new tsregisterDao(this.data.WorkId);

    }

    //获取科室
    depttree() {
        //return this.hisinterface.getdept();
        return this.dao.getdept();
    }

    //获取医生列表
    getDoctorList() {
        var pageInfo = {
            key: "Id desc",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "10"),
            totalRecord: 100
        };
        var search = TSLobsterAPI.Program.RequestQuery("search", "");

        return this.dao.getDoctorPageList(pageInfo, search);
    }

    //医生信息
    doctorInfo() {
        let deptId = TSLobsterAPI.Program.RequestQuery("deptId");
        let doctId = TSLobsterAPI.Program.RequestQuery("doctId");

        let data = this.dao.getDoctorInfo(parseInt(doctId));
        return { info: data };
    }


    //从his接口获取科室排班数据
    getAllSchedule() {
        let deptId = TSLobsterAPI.Program.RequestQuery("deptId");
        // let allschedulelist = this.hisinterface.getdocOrderSched(deptId);
        let allschedulelist = this.dao.getdocOrderSched(0, deptId);
        //存入缓存
        TSLobsterAPI.Utils.setsessioncache('allschedulelist_' + deptId, JSON.stringify(allschedulelist));
        return { list: allschedulelist };
    }

    //获取单日医生排班数据
    daySchedule() {

        let daytime = TSLobsterAPI.Program.RequestQuery("date", "");
        let deptId = TSLobsterAPI.Program.RequestQuery("deptId");
        let alldoctorlist = this.dao.getDoctorList(parseInt(deptId)) || [];
        let allschedulelist = JSON.parse(TSLobsterAPI.Utils.getsessioncache('allschedulelist_' + deptId));
        var scheduledata = [];
        if (daytime == "") { //全部
            alldoctorlist.forEach(item => {
                this.vo_doctorlist.DeptId = item.DeptId;
                this.vo_doctorlist.DoctId = item.EmpId;
                this.vo_doctorlist.RegDate = '';
                this.vo_doctorlist.DeptName = item.DeptName;
                this.vo_doctorlist.DocName = item.EmpName;
                this.vo_doctorlist.Describe = item.Describe;
                this.vo_doctorlist.RankName = item.DoctorPost;
                this.vo_doctorlist.HeadImg = item.HeadImg;

                let result = this.service.countSchedule(allschedulelist, item.EmpId);//计算
                this.vo_doctorlist.IsHave = result.IsHave;
                this.vo_doctorlist.IsFull = result.IsFull;//计算
                this.vo_doctorlist.HaveCount = result.HaveCount;//计算
                this.vo_doctorlist.AllCount = result.AllCount;

                scheduledata.push(JSON.parse(JSON.stringify(this.vo_doctorlist)));
            })
        }
        else {
            alldoctorlist.forEach(item => {
                this.vo_doctorlist.DeptId = item.DeptId;
                this.vo_doctorlist.DoctId = item.EmpId;
                this.vo_doctorlist.RegDate = daytime;
                this.vo_doctorlist.DeptName = item.DeptName;
                this.vo_doctorlist.DocName = item.EmpName;
                this.vo_doctorlist.Describe = item.Describe;
                this.vo_doctorlist.RankName = item.DoctorPost;
                this.vo_doctorlist.HeadImg = item.HeadImg;

                let result = this.service.countSchedule(allschedulelist, item.EmpId, daytime);//计算
                this.vo_doctorlist.IsHave = result.IsHave;
                this.vo_doctorlist.IsFull = result.IsFull;//计算
                this.vo_doctorlist.HaveCount = result.HaveCount;//计算
                this.vo_doctorlist.AllCount = result.AllCount;

                scheduledata.push(JSON.parse(JSON.stringify(this.vo_doctorlist)));
            })
        }

        return { list: scheduledata };
    }


    //单个医生排班数据
    doctorSchedule() {
        let deptId = TSLobsterAPI.Program.RequestQuery("deptId");
        let doctId = TSLobsterAPI.Program.RequestQuery("doctId");

        var weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
        var scheduledata = [];
        let docschedule = this.hisinterface.getdocOrderSched(0, doctId) || [];
        if (docschedule.length > 0) {
            docschedule[0].ItemA.forEach(item => {
                this.vo_doctorschedule.RegId = item.RegId;
                this.vo_doctorschedule.DeptId = item.DeptId;
                this.vo_doctorschedule.DoctId = item.DoctId;
                this.vo_doctorschedule.DeptName = item.DeptName;
                this.vo_doctorschedule.DocName = item.DocName;
                this.vo_doctorschedule.RegDate = item.RegDate;
                this.vo_doctorschedule.TimeType = item.TimeType;
                this.vo_doctorschedule.RegFee = item.RegFee;
                this.vo_doctorschedule.Week = weekday[item.Week];
                this.vo_doctorschedule.IsFull = 0;
                this.vo_doctorschedule.IsHave = 0;
                this.vo_doctorschedule.AllCount = 0;
                this.vo_doctorschedule.HaveCount = 0;
                if (item.AllCount > 0) {
                    this.vo_doctorschedule.IsHave = 1;
                    this.vo_doctorschedule.AllCount = item.AllCount;
                    this.vo_doctorschedule.HaveCount = item.HaveCount;
                    if (item.HaveCount == 0) {
                        this.vo_doctorschedule.IsFull = 1;
                    }
                }
                scheduledata.push(JSON.parse(JSON.stringify(this.vo_doctorschedule)));
            })
        }

        return { list: scheduledata };
    }

    //预约挂号，获取医生排班时段
    //互联网问诊，获取医生排班时段
    dayTimeRange() {
        //let daytime = TSLobsterAPI.Program.RequestQuery("regdate", "");
        //schedtype 0 线下预约挂号 1线上互联网问诊
        let schedtype = TSLobsterAPI.Program.RequestQuery("schedtype");
        let deptId = TSLobsterAPI.Program.RequestQuery("deptId");
        let doctId = TSLobsterAPI.Program.RequestQuery("doctId");
        let nowdate = TSLobsterAPI.Utils.getCurrTime('yyyy-MM-dd');

        let allschedulelist = this.dao.getdocOrderSched(schedtype, deptId, doctId);
        //找出医生索引
        //let index = allschedulelist.map(x => { return x.DoctId; }).indexOf(parseInt(doctId));
        var timeRange = [];
        if (allschedulelist.length > 0) {
            allschedulelist[0].ItemA.forEach(item => {
                //获取时段
                //var rangelist = this.hisinterface.scheduleTimeRange(item.RegId) || [];
                var rangelist = this.dao.scheduleTimeRange(item.RegId) || [];
                rangelist.forEach(range => {
                    this.vo_doctorrange.DeptId = parseInt(deptId);
                    this.vo_doctorrange.DoctId = parseInt(doctId);
                    this.vo_doctorrange.RegDate = item.RegDate;
                    this.vo_doctorrange.RegId = item.RegId;
                    this.vo_doctorrange.Timeid = range.Timeid;
                    this.vo_doctorrange.StartTime = range.StartTime;
                    this.vo_doctorrange.EndTime = range.EndTime;
                    this.vo_doctorrange.IsAll = 1;
                    this.vo_doctorrange.Status = item.scheduleStatus;
                    this.vo_doctorrange.TimeType = item.TimeType;
                    this.vo_doctorrange.Week = item.Week;
                    this.vo_doctorrange.RegFee = item.RegFee;
                    this.vo_doctorrange.DeptName = item.DeptName;
                    this.vo_doctorrange.DocName = item.DocName;
                    this.vo_doctorrange.IsHave = 0;
                    this.vo_doctorrange.HaveCount = 0;
                    this.vo_doctorrange.AllCount = 0;
                    this.vo_doctorrange.IsExpire = 0;
                    if (range.HaveCount > 0) {
                        this.vo_doctorrange.IsHave = 1;
                        this.vo_doctorrange.HaveCount = range.HaveCount;
                        this.vo_doctorrange.AllCount = range.AllCount;
                    }
                    if (item.RegDate == nowdate) {
                        if (item.TimeType == 1 && new Date() > new Date(nowdate + ' 12:00:00')) {
                            this.vo_doctorrange.IsExpire = 1;
                        }
                        else if (item.TimeType == 2 && new Date() > new Date(nowdate + ' 18:00:00')) {
                            this.vo_doctorrange.IsExpire = 1;
                        }
                    }
                    timeRange.push(JSON.parse(JSON.stringify(this.vo_doctorrange)));
                })

            })
        }
        return { list: timeRange };
    }

    /**
     * 预约挂号
     * 1、生成挂号记录，状态为待付款
     * 2、调用支付系统，付款
     * 3、调用his系统接口，挂号
     * 4、更新挂号记录，状态为已付款
     */
    register() {
        var model = JSON.parse(TSLobsterAPI.Program.RequestBody());

        return this.service.register(model);
    }

    /**
     * 我的挂号记录
     */
    getregrecordlist() {
        var pageInfo = {
            key: "Id desc",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "10"),
            totalRecord: 100
        };
        // var status = TSLobsterAPI.Program.RequestQuery('status');
        var patientId = TSLobsterAPI.Program.RequestQuery('PatientId', '0');
        var userId = TSLobsterAPI.Program.GetMPUserId();

        return this.dao.getregrecordlist(userId, patientId, pageInfo);
    }

    /**
     * 问诊记录
     */
    getmedrecordlist() {
        var pageInfo = {
            key: "Id desc",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "10"),
            totalRecord: 100
        };
        //status 0问诊中 1已完成
        var status = TSLobsterAPI.Program.RequestQuery('status');
        var patientId = TSLobsterAPI.Program.RequestQuery('PatientId', '0');
        var userId = TSLobsterAPI.Program.GetMPUserId();

        return this.dao.getmedrecordlist(status, userId, patientId, pageInfo);
    }

    /**
     * 获取挂号记录详情
     */
    getRegisterInfo() {
        var id = TSLobsterAPI.Program.RequestQuery('id');
        var info = this.dao.getRegisteredInfo(parseInt(id));
        return { info: info };
    }

    /**
     * 取消预约
     * 1、更新挂号记录中状态为退款中
     * 2、调用his接口，取消挂号记录
     * 3、调用支付系统，退款
     * 4、更新挂号记录中状态为退款完成
     */
    cancelregister() {
        var id = TSLobsterAPI.Program.RequestQuery('id');

        return this.service.cancelregister(id);
    }

    /**
     * Web后台获取预约挂号记录
     */
    getregrecordlist_web() {
        var pageInfo = {
            key: "Id desc",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "10"),
            totalRecord: 100
        };
        var sdate = TSLobsterAPI.Program.RequestQuery('sdate');
        var edate = TSLobsterAPI.Program.RequestQuery('edate');
        var status = TSLobsterAPI.Program.RequestQuery('status');//0全部 1待支付 2已挂号 3已退款 4已取消
        var keyword = TSLobsterAPI.Program.RequestQuery('keyword') || '';

        return this.dao.getregrecordlist_web(sdate, edate, status, keyword, pageInfo);
    }

    /**
     * 医生信息界面，显示点赞最多的评价
     */
    getevaluationtopone() {
        let doctId = TSLobsterAPI.Program.RequestQuery("doctId");
        var data = this.dao.getevaluationtopone(doctId);
        return { data: data };
    }

    /**
     * 更新评价点赞数
     */
    updateevaluationgivecount() {
        let id = TSLobsterAPI.Program.RequestQuery("id");
        this.dao.updateevaluationgivecount(id);
        return { result: true };
    }

    testsysconfig() {
        let appid = TSLobsterAPI.Program.GetAppId();
        let workid = TSLobsterAPI.Program.GetWorkId();
        let ret = TSLobsterAPI.AppFunction.GetSystemConfigs(workid, appid, 'SubMchId');
        return ret;
    }
}